import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

import 'package:latlong2/latlong.dart';
import 'package:macless_haystack/platform_stub.dart'  
    if (dart.library.html) 'package:macless_haystack/platform_web.dart' as web;
import 'package:map_launcher/map_launcher.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:url_launcher/url_launcher.dart';

class MapOpener {
  
  static bool isIOS() {
    final userAgent = web.getUserAgent().toLowerCase();
    return userAgent.contains('iphone') ||
        userAgent.contains('ipad') ||
        userAgent.contains('ipod');
  }

  static bool isAndroid() {
    final userAgent = web.getUserAgent().toLowerCase();
    return userAgent.contains('android');
  }

  static openMapsSheet(context, LatLng location, String title) async {
    try {
      if (kIsWeb) {
        List<Map<String, dynamic>> maps = [];
        if (isIOS()) {
          // final String iosMapsUrl = 'maps://?ll=${location.latitude},${location.longitude}';
          final Uri iosMapsUrl = Uri.parse(
              'https://maps.apple.com/?q=$title&sll=${location.longitude},${location.latitude}&z=10&t=m');
          maps.add({'type': 'apple', 'name': '苹果地图', 'url': iosMapsUrl});
          
          final Uri iosAMapUrl = Uri.parse(
              'iosamap://navi?sourceApplication=maclesshaystack&poiname=${Uri.encodeComponent(title)}&lat=${location.latitude}&lon=${location.longitude}&dev=0');
          maps.add({'type': 'amap', 'name': '高德地图', 'url': iosAMapUrl});
        } else if (isAndroid()) {
          final Uri androidAMapUrl = Uri.parse(
              'androidamap://navi?sourceApplication=maclesshaystack&poiname=${Uri.encodeComponent(title)}&lat=${location.latitude}&lon=${location.longitude}&dev=0');
          maps.add({'type': 'amap', 'name': '高德地图', 'url': androidAMapUrl});
        }
        final Uri baiduMapUrl = Uri.parse(
            'baidumap://map/marker?location=${location.latitude},${location.longitude}&title=${Uri.encodeComponent(title)}&coord_type=wgs84&src=webapp');
        maps.add({'type': 'baidu', 'name': '百度地图', 'url': baiduMapUrl});

        if(maps.isNotEmpty) {
          showModalBottomSheet(
            context: context,
            builder: (BuildContext context) {
              return SafeArea(
                child: SingleChildScrollView(
                  child: Container(
                    child: Wrap(
                      children: <Widget>[
                        for (var map in maps)
                          ListTile(
                            onTap: () => launchUrl(map['url']),
                            title: Text(map['name']),
                            leading: SvgPicture.asset(
                              'packages/map_launcher/assets/icons/${map['type']}.svg',
                              height: 30.0,
                              width: 30.0,
                            ),
                          ),
                      ],
                    ),
                  ),
                ),
              );
            },
          );
        }
      } else {
        final coords = Coords(location.latitude, location.longitude);
        final availableMaps = await MapLauncher.installedMaps;

        showModalBottomSheet(
          context: context,
          builder: (BuildContext context) {
            return SafeArea(
              child: SingleChildScrollView(
                child: Container(
                  child: Wrap(
                    children: <Widget>[
                      for (var map in availableMaps)
                        ListTile(
                          onTap: () => map.showMarker(
                            coords: coords,
                            title: title,
                          ),
                          title: Text(map.mapName),
                          leading: SvgPicture.asset(
                            map.icon,
                            height: 30.0,
                            width: 30.0,
                          ),
                        ),
                    ],
                  ),
                ),
              ),
            );
          },
        );
      }
    } catch (e) {
      print(e);
    }
  }

}